package com.github.cooingandwooing.manager;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.cooingandwooing.entity.Example2ProductEntity;
import com.github.cooingandwooing.mapper.Example2ProductMapper;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author cooingandwooing
 */
@Transactional
@Service
public class Example2UserManager {

    @Resource
    private Example2ProductMapper example2ProductMapper;

    public void updateByEntitySucc() {
        System.out.println("当前库存为：");
        QueryWrapper<Example2ProductEntity> ew = new QueryWrapper<>();
        ew.eq("id", 3477374334L);
        Long id;
        Example2ProductEntity goodA = example2ProductMapper.selectOne(ew);
        Example2ProductEntity goodB = example2ProductMapper.selectOne(ew);
        id = goodB.getId();
        goodA.setNum(goodA.getNum() + 50);
        example2ProductMapper.updateById(goodA);
        goodB.setNum(goodB.getNum() - 30);
        int result = example2ProductMapper.updateById(goodB);

        if (result == 0) {
            Example2ProductEntity goodC = example2ProductMapper.selectById(goodB.getId());
            goodC.setNum(goodC.getNum() - 30);
            example2ProductMapper.updateById(goodC);
        }
        Example2ProductEntity newEntity = example2ProductMapper.selectById(id);
        System.out.println("当前库存为：" + newEntity.getNum());
    }
}
